require('../css/my.less');
require('../css/commom/basic.css');
document.ready(function () {


    utils.footerAll('my');
    let leaveDom = document.querySelector('.leave');
    let userName = document.querySelector(".userName");
    let avater = document.querySelector('.avater');
    let stageDom = document.querySelector('.stage');
    let generalNumDom = document.querySelector('.generalNum');
    let consumeNumDom = document.querySelector('.consumeNum');
    let fileBtn = document.querySelector('.fileBtn');
    let boxTopS = document.querySelector('.boxTopS');
    let http = 'http://139.9.177.51:8099';

    // 退出登录

    leaveDom.addEventListener('click', function () {
        // 清除本地存储
        localStorage.removeItem('user');
        location.href = './login.html';
    })

    // 点击跳转个人信息页面
    userName.addEventListener('click', function (ev) {
        location.href = './edit.html';
    })

    //获取用户信息
    
    let user = JSON.parse(localStorage.getItem('user'));
    function getUser() {
        $http.get('/users/accountinfo?userId=' + user.userId, function(res) {
            localStorage.setItem('user', JSON.stringify(res.data));
            // 用户头像
            console.log(res.data.imgurl);
            if (res.data.imgurl) {
                avater.src = res.data.imgurl;
            };
            // 用户名字
            if (res.data.nickname) {
                userName.innerHTML = res.data.nickname;
            };
            // 个性签名
            if (res.data.sign) {
                stageDom.innerHTML = res.data.sign;
            };
        })

    }
    getUser();

    // 运动信息
    $http.get('/users/mysportsBadge?userId=' + user.userId, function (res) {
        generalNumDom.innerHTML = res.data.sports.times;
        consumeNumDom.innerHTML = res.data.sports.calorie;
    })
    // 监听input 请求上传头像ajax接口
    fileBtn.addEventListener('change', function () {
        // 修改头像的ajax
        $updateFile('/users/upload', 'imgurl', this.files[0], function (res1) {
            // 头像地址
            let imgUrl1 = http + res1.data;
            user.imgurl = imgUrl1;
            alterUser(imgUrl1);
            localStorage.setItem('user', JSON.stringify(user));
            avater.src = user.imgurl;
        })
    })
    //调用修改数据
    function  alterUser(url){

        let data = {
            userId: user.userId,
            imgurl: url
        }
        $http.post('/users/userEdit', data, function (res) {
            if (res.status == 0) {
                getUser();
            }
        })
    }
    // 点击更换头像
    avater.addEventListener('click', function () {
        // 绑定上传文件按钮的点击事件
        fileBtn.click();


        console.log(user.imgurl);
    })


    // 跳转运动数据
    boxTopS.addEventListener('click',function(){
        location.href='./myData.html'
    })

})
